﻿using System.Configuration;
using System.Linq;
using System.Reflection;
using Autofac;

namespace BLL
{
    /// <summary>
    /// Provides autofac configuration logic for services and their interfaces.
    /// </summary>
    public class BLLModule : Autofac.Module
    {
        /// <summary>
        /// Adds services registrations to the containers.
        /// </summary>
        /// <param name="builder">Container builder to add registrations to.</param>
        protected override void Load(ContainerBuilder builder)
        {
            var servicesAssembly = Assembly.GetExecutingAssembly();
            var connectionString = ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString;
            builder.RegisterAssemblyTypes(servicesAssembly)
                .Where(t => (t.Name.EndsWith("Service") || t.Name.EndsWith("AuditProxy")) && t.IsClass)
                .As(t => t.GetInterfaces()
                .Where(i => i.Name.EndsWith("Service")))
                .InstancePerDependency();
            builder.RegisterAssemblyTypes(servicesAssembly).Where(t => (t.Name.EndsWith("Collection") && t.IsClass))
                .AsSelf()
                .WithParameter("connectionString",connectionString)
                .InstancePerDependency();

        }
    }
}